<template>
  <div class="user-page-container">
    <Header />
    <div class="user-page-wrapper">
      <div class="user-page-content">
        <el-row :gutter="30">
          <!-- 左侧导航菜单 -->
          <el-col :span="6">
            <div class="left-sidebar">
              <div class="user-info-card">
                <el-card>
                  <div class="user-profile">
                    <el-avatar :size="80" src="../../assets/images/default-avatar.png" />
                    <h3>{{ username }}</h3>
                    <p class="user-role">咨询者</p>
                  </div>
                </el-card>
              </div>
              
              <el-card class="menu-card">
                <el-menu
                  :default-active="activeMenu"
                  class="user-menu"
                  @select="handleMenuSelect"
                >
                  <el-menu-item index="dashboard">
                    <el-icon><HomeFilled /></el-icon>
                    <span>首页</span>
                  </el-menu-item>
                  <el-menu-item index="profile">
                    <el-icon><User /></el-icon>
                    <span>个人信息</span>
                  </el-menu-item>
                  <el-menu-item index="appointments">
                    <el-icon><Calendar /></el-icon>
                    <span>预约管理</span>
                  </el-menu-item>
                  <el-menu-item index="history">
                    <el-icon><List /></el-icon>
                    <span>咨询历史</span>
                  </el-menu-item>
                  <el-menu-item index="my-questions">
                    <el-icon><ChatLineSquare /></el-icon>
                    <span>我的问答</span>
                  </el-menu-item>
                </el-menu>
              </el-card>
            </div>
          </el-col>

          <!-- 右侧内容区域 -->
          <el-col :span="18">
            <el-card class="content-card">
              <router-view></router-view>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>
    <Footer />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import Header from '../../components/header.vue'
import Footer from '../../components/footer.vue'
import { User, Calendar, List, HomeFilled, ChatLineSquare } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const activeMenu = ref(router.currentRoute.value.path)
const username = ref(localStorage.getItem('username') || '')

// 用户信息
const userInfo = ref({
  name: '张三',  // 这里应该从后端获取用户信息
  avatar: '',
  role: 'consultant'
})

// 处理菜单选择
const handleMenuSelect = (key: string) => {
  switch (key) {
    case 'dashboard':
      router.push('/user/dashboard')
      break
    case 'profile':
      router.push('/user/message')
      break
    case 'appointments':
      router.push('/user/appointment')
      break
    case 'history':
      router.push('/user/history')
      break
    case 'test-history':
      router.push('/user/test-history')
      break
    case 'my-questions':
      router.push('/user/my-questions')
      break
  }
}

// 根据当前路由设置活动菜单项
onMounted(() => {
  const path = route.path
  if (path.includes('dashboard')) {
    activeMenu.value = 'dashboard'
  }
  if (path.includes('message')) {
    activeMenu.value = 'profile'
  } else if (path.includes('appointment')) {
    activeMenu.value = 'appointments'
  } else if (path.includes('history')) {
    activeMenu.value = 'history'
  } else if (path.includes('test-history')) {
    activeMenu.value = 'test-history'
  } else if (path.includes('my-questions')) {
    activeMenu.value = 'my-questions'
  }
})
</script>

<style scoped>
.user-page-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.user-page-wrapper {
  width: 100%;
  background-color: #f5f7fa;
  min-height: calc(100vh - 70px);  /* 减去header高度 */
  padding-top: 84px;  /* header下方间距 */
}

.user-page-content {
  max-width: 1400px;
  margin: 0 auto 40px;
  padding: 0 40px;
}

.left-sidebar {
  position: sticky;
  top: 100px;
  padding: 4px;  /* 添加内边距 */
}

.user-info-card {
  margin-bottom: 20px;
}

.user-profile {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
}

.user-profile h3 {
  margin: 16px 0 8px;
  font-size: 18px;
  color: #2c3e50;
}

.user-role {
  color: #909399;
  font-size: 14px;
  margin: 0;
}

.menu-card {
  position: sticky;
  top: 100px;
  margin-bottom: 20px;
}

.user-menu {
  border-right: none;
}

.content-card {
  min-height: 600px;
  padding: 10px;
}

:deep(.el-menu-item) {
  display: flex;
  align-items: center;
  height: 52px;
  margin: 3px 0;
  border-radius: 8px;
  padding: 0 20px !important;
}

:deep(.el-menu-item.is-active) {
  background-color: #ecf5ff;
}

:deep(.el-menu-item .el-icon) {
  margin-right: 12px;
  font-size: 18px;
}

:deep(.el-menu-item span) {
  font-size: 15px;
}

:deep(.el-card) {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  border: none;  /* 移除边框 */
}

:deep(.el-card__body) {
  padding: 20px;
}

:deep(.el-row) {
  margin-left: 0 !important;
  margin-right: 0 !important;
}

:deep(.el-col) {
  padding-left: 15px !important;
  padding-right: 15px !important;
}
</style>
